package com.base.cn.platform.os.entity.course.cloud;

import com.base.cn.platform.os.common.utils.ObjectUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Data
@EqualsAndHashCode(callSuper = false)
public class CloudService<T> {

    private int code;
    private List<T> data;
    private int total;
    private String msg;
    private String idvarchar;

    public List<T> getData() {
        if (data == null) {
            return Collections.emptyList();
        }
        return data.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    public Optional<T> getFirst() {
        List<T> data = this.getData();
        if (ObjectUtils.isEmpty(data)) {
            return Optional.empty();
        } else {
            return Optional.of(data.get(0));
        }
    }
}
